package com.bwie.task;

import com.bwie.domain.TbProd;
import com.bwie.mapper.TbProdMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2024/10/18 16:19
 */

@Component
@Slf4j
public class TaskTest {

    @Autowired
    TbProdMapper tbProdMapper;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void test(){
        log.info("定时任务");

        long now = new Date().getTime();
        List<TbProd> tbProds = tbProdMapper.selectList(null);
        for (TbProd tbProd : tbProds) {
            // 获取商品过期时间
            long time = tbProd.getCreateTime().getTime();

            if(time < now) {
                log.info("商品过期了，id为：{}", tbProd.getProdId());
                // 设置过期
                tbProd.setProdStatus(1);
                // 更新数据库
                tbProdMapper.updateById(tbProd);
            }
        }

    }

}
